package com.lanou.tipsy.fragment.singer.classify;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lanou.tipsy.R;
import com.lanou.tipsy.base.BaseFragment;
import com.lanou.tipsy.base.Constants;


/**
 * Created by FengJun on 15/10/21.
 * 歌手-欧美
 */
public class EuropeAmericaFragment extends BaseFragment {
    public static final String TAG = "EuropeAmericaFragment";
    private Context context;
    private TabLayout mTabLayout;//定义一个tabLayout
    private ViewPager mViewPager;//定义一个viewPager

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fr_singer_chinese, null);
        initView(view);
        return view;
    }

    private void initView(View view) {
        mTabLayout = (TabLayout) view.findViewById(R.id.tl_classify);
        mViewPager = (ViewPager) view.findViewById(R.id.vp_classify);
        MyAdapter adapter = new MyAdapter(getFragmentManager());
        mViewPager.setAdapter(adapter);
        mTabLayout.setupWithViewPager(mViewPager);
        mTabLayout.setSelectedTabIndicatorColor(getResources().getColor(R.color.color_grey));
        mTabLayout.setTabTextColors(getResources().getColor(R.color.color_grey),
                getResources().getColor(R.color.color_black));
    }

    public class MyAdapter extends FragmentStatePagerAdapter {
        private String[] titles = {"男歌手", "女歌手", "组合"};
        private String[] urls = {Constants.SINGERS_EU_MALE, Constants.SINGERS_EU_FEMALE, Constants.SINGERS_EU_GROUP};

        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            SingersClassifyFragment fragment = SingersClassifyFragment.getFragment(urls[position]);
            if (fragment instanceof ControUrl) {
                controUrl = (ControUrl) fragment;
                controUrl.getUrls(urls);
            }
            return fragment;
        }

        @Override
        public int getCount() {
            return titles.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }
    }

    private ControUrl controUrl;

    public interface ControUrl {
        void getUrls(String[] urls);
    }
}
